package com.lanou3g.controller;

import com.lanou3g.entity.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
//本页知识点,接受请求参数
@Controller
@RequestMapping("/user")
//在这加@RequestMapping("/user"),此后访问这个类时,路径变为/user/...
public class UserController {
    @RequestMapping("/login")
    public String login(@RequestParam(value = "username", required = true, defaultValue = "张三") String usme, String password, int age,
                        @DateTimeFormat(pattern = "yyyy-MM-dd") Date birthday) {
        //required = true表示这个值不为空
        //defaultValue = "张三"表示设置的默认值为张三
        //@DateTimeFormat(pattern = "yyyy-MM-dd")设置日期时间格式
        System.out.println(usme);
        System.out.println(password);
        System.out.println(age);
        System.out.println(birthday);
        return null;
    }

    @RequestMapping("/login1")
    public String login1(User user) {
        System.out.println(user.getAge());
        System.out.println(user.getPassword());
        System.out.println(user.getUsername());
        System.out.println(user.getBirthday());
        return null;
    }

    @RequestMapping("/test")
    public String test(HttpServletRequest request, HttpServletResponse response,
                       HttpSession session, Model model) throws ServletException, IOException {
        model.addAttribute("msg", "呵呵呵");//相当于request.setAttribute("msg","呵呵呵")
        session.setAttribute("msg", "哈哈哈");
        //转发
        // request.getRequestDispatcher("/1.jsp").forward(request, response);
        //return "forword:1";//转发spring封装写法,转发forword关键字可省
        //请求重定向
        //response.sendRedirect("/1.jsp");
        return "redirect:/1.jsp";//请求重定向spring封装写法;这块儿不能像forword一样直接写1,否则报错
    }

    @RequestMapping(value = {"/get", "/get1"}, method = RequestMethod.GET)
    //method = RequestMethod.GET要求必须是get请求
    public String get() {
        return null;
    }

    @RequestMapping("/{id}/{name}")
    public void findById(@PathVariable int id, @PathVariable String name) {
        //@PathVariable int id  将id变成路径的一部分,然后再在@RequestMapping的()里加上{id}
        //要在路径上传多个参数,可以多个拼接,例如/{id}/{name}或者放在一个大括号里{"/{id}","/{name}"}
        System.out.println(id);
        System.out.println(name);
    }
}
